package cn.ex.ymcc.service.impl;

import cn.ex.ymcc.config.SMSVerifyCodeProperties;
import cn.ex.ymcc.constants.BaseConstants;
import cn.ex.ymcc.constants.MineError;
import cn.ex.ymcc.constants.SmsCodeConstants;
import cn.ex.ymcc.domain.Blacklist;
import cn.ex.ymcc.domain.MessageSms;

import cn.ex.ymcc.dto.RedisRegisterDto;
import cn.ex.ymcc.service.IMessageSmsService;
import cn.ex.ymcc.service.IVerifyCodeService;
import cn.ex.ymcc.util.AssertUtil;
import cn.ex.ymcc.util.GetIpUtil;
import cn.ex.ymcc.util.StrUtils;
import cn.ex.ymcc.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: ZeWangmu
 * @Date: 2022/06/15/7:31
 * @Description: 生成验证码的实现类
 */
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    //加泛型是为了拿已有的实例
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private IMessageSmsService messageSmsService;

    @Autowired
    private SMSVerifyCodeProperties smsVerifyCodeProperties;

    @Autowired
    private SmsCodeConstants smsCodeConstants;



    /**
     * 发送图形验证码
     * @param key
     * @return
     */
    @Override
    public String imageCode(String key) {
        AssertUtil.isNotEmpty(key,MineError.NO_EMPTY_SECURITY_CODE.getMessage());

        //设置图形验证码长度，调用getComplexRandomString可生成任意的字母，数字字符串
        String code = StrUtils.getComplexRandomString(4);
        //，保存redis，设置redis过期时间
        redisTemplate.opsForValue().set(key,code, 5,TimeUnit.MINUTES);
        //返回base64编码的字符串
        return VerifyCodeUtils.verifyCode(100,40,code);

    }

    /**
     * 发送短信验证码
     * @param mobile
     */
    @Override
    public void getSmsCode(String mobile) {

        //校验手机号
        AssertUtil.isNotEmpty(mobile,MineError.COMMON_PHONE_NO_EMPTY.getMessage());

        //生成短信验证码,设置存储的key
        String smsKey = String.format(BaseConstants.User.REGISTER_PHONE_PRIFIX , mobile);
        //查询短信验证码
        Object SmsCodeTmp = redisTemplate.opsForValue().get(smsKey);

        Date now = new Date();

        String smsCode = null;
        if(SmsCodeTmp != null){
            //2.从redis拿到了
            RedisRegisterDto registerDto = (RedisRegisterDto) SmsCodeTmp;
            boolean canSend = (now.getTime() - registerDto.getTime()) / 1000 > smsCodeConstants.getInterval();
            AssertUtil.isTrue(canSend,MineError.COMMON_SERIOUS.getMessage());
            //过了重发时间，使用还未过期的码儿
            smsCode = registerDto.getCode();
        }else{
            //生成新的验证码
            smsCode = StrUtils.getRandomString(smsCodeConstants.getSize());
        }
        //储存验证码并设置过期时间
        String smsCodeValue = smsCode + ":" + System.currentTimeMillis();
        redisTemplate.opsForValue().set(smsKey,
                smsCodeValue,
                smsVerifyCodeProperties.getExpire(),
                TimeUnit.MINUTES);
        /*//发送短信
        SmsUtils.sendSms(phone,"亲，您的验证码是："+smsCode+"，在三分钟内有效");*/
        System.out.println(smsCode);

        String message = "您的短信验证码是:"+smsCode;
        saveMessageSms(now, smsCode, message);

    }

    /**
     * 保存发送短信的记录表
     * @param now
     * @param smsCode
     * @param message
     */
    private void saveMessageSms(Date now, String smsCode, String message) {
        MessageSms messageSms = new MessageSms();
        messageSms.setContent(message);
        messageSms.setSendTime(now);
        messageSms.setTitle(smsVerifyCodeProperties.getTitle());
        messageSms.setIp(GetIpUtil.getIp());//获取IP地址
        messageSmsService.insert(messageSms);
    }


}
